package com.xyq.system.service.sysuser.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xyq.common.core.domain.LoginUser;
import com.xyq.common.core.domain.Result;
import com.xyq.common.core.domain.vo.LoginUserVO;
import com.xyq.common.core.enums.ResultCode;
import com.xyq.common.core.enums.UserIdentity;
import com.xyq.common.core.exception.ServiceException;
import com.xyq.common.sucurity.service.TokenService;
import com.xyq.system.domain.sysuser.SysUser;
import com.xyq.system.mapper.sysuser.SysUserMapper;
import com.xyq.system.service.sysuser.ISysUserService;
import com.xyq.system.utils.BCryptUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RefreshScope
public class SysUserServiceImpl implements ISysUserService {

    @Value("${jwt.secret}")
    private String secret ;

    @Autowired
    private TokenService tokenService ;

     @Autowired
     private SysUserMapper sysUserMapper ;

    @Override
    public Result<String> login(String userAccount, String password) {
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
         // 相当于
        // select password from tb_system_user where user_account = #{userAccount}
        SysUser sysUser = sysUserMapper.selectOne(queryWrapper.
                select( SysUser::getUserId, SysUser::getPassword,SysUser::getNickName).eq(SysUser::getUserAccount,userAccount));
//          Result loginResult = new Result<>();
         if(sysUser == null ){
                 return Result.fail(ResultCode.FAILED_USER_NOT_EXISTS) ;
         }else if(BCryptUtils.matchesPassword(password , sysUser.getPassword())){

              return Result.success(tokenService.createToken(sysUser.getUserId() , secret , UserIdentity.ADMIN.getValue(),sysUser.getNickName() , null));
         }
         // 身份验证失败

        return Result.fail(ResultCode.FAILED_LOGIN);
    }

    @Override
    public int add(String userAccount, String password) {
          // 判断用户是否重复
        List<SysUser> sysUsers = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>()
                .eq(SysUser::getUserAccount, userAccount));   // 找是否有账号== userAccount的
        //  判断
        if(!sysUsers.isEmpty() ){
            // 抛一个自定义异常出去
           throw new ServiceException(ResultCode.FAILED_USER_EXISTS);
        }

        SysUser sysUser = new SysUser() ;
           sysUser.setUserAccount(userAccount) ;
           sysUser.setPassword(BCryptUtils.encryptPassword(password));  // 加密密码
           Integer row = sysUserMapper.insert(sysUser);
        return row ;
    }

    @Override
    public Result info(String token) {
        LoginUser loginUser = tokenService.getLoginUser(token , secret) ;
        if(loginUser == null){
            return Result.fail();
        }
        LoginUserVO loginUserVO = new LoginUserVO() ;
        loginUserVO.setNickName(loginUser.getNickName());
        return Result.success(loginUserVO) ;
    }

    @Override
    public boolean logout(String token) {
        // 1.验证登录信息
        return tokenService.deleteLoginUser(token , secret);
    }

}
